package com.sofwin.controller;/**
 * @PackageName:com.sofwin.controller
 * @author:andyLiu
 * @DateTime:2020/12/28 16:04
 * @Description:[TODO]输入当前模块作用
 **/

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sofwin.dto.MenuDto;
import com.sofwin.dto.TreeNodeDto;
import com.sofwin.pojo.SResource;
import com.sofwin.pojo.SRoleresource;
import com.sofwin.pojo.SUser;
import com.sofwin.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ResourceController
 * @Description: TODO
 * @Author: andyliu
 * @Date: 2020/12/28 16:04
 * @Version: 1.0
 **/
@Controller()
@RequestMapping("/resources")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @RequestMapping("data")
    @ResponseBody
    public List<TreeNodeDto> data(){
        List<TreeNodeDto> list = resourceService.queryAll();
        return list;
    }

    @RequestMapping("/json/data")
    @ResponseBody
    public List<SResource> jsonData(){
        List<SResource> sResources = resourceService.queryAllResourceForJson();
        return sResources;
    }


    /**
     *
     * @param model
     * @param id  角色id
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("/toPermission")
    public String toPermission(Model model,Integer id) throws JsonProcessingException {
        // map key id pid name open
        // 查询所有的资源
//        List<Map> result = resourceService.queryAllResources();
        List<SResource> result = resourceService.queryAllResourceByRoleId(id);
        ObjectMapper mapper = new ObjectMapper();
        // 将object对象或集合转为json对象或json数组
        String str = mapper.writeValueAsString(result);
        model.addAttribute("result",str);
        model.addAttribute("roleId",id);
        return "role/permission";
    }
    @RequestMapping("/savePermission")
    @ResponseBody
    public Map savePermission(SRoleresource roleresource){
        boolean status = resourceService.save(roleresource);
        Map map = new HashMap();
        map.put("status",status);
        return map;
    }
    @RequestMapping("/savePermission2")
    @ResponseBody
    public Map savePermission2(Integer[] ids,Boolean status,Integer roleId){
        Map map = new HashMap();
        status = resourceService.saveRoleResources(ids,status,roleId);
        map.put("status",status);
        return map;
    }

    /**
     * 通过角色id 返回左侧该角色对应的资源信息
     * @param roleId
     * @return
     */
    @RequestMapping("/roleResources")
    @ResponseBody
    public List<MenuDto> roleResources(HttpSession session){
        SUser user =(SUser)session.getAttribute("user");
        Integer roleId = user.getRoleid();
        List<MenuDto> menus = resourceService.queryResourcesByRoleId(roleId);
        return menus;
    }

}
